package zzr.library;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.snackbar.Snackbar;

import java.time.Duration;
import java.time.LocalDateTime;

import zzr.library.databinding.ActivityMainBinding;
import zzr.library.databinding.DialogLoginBinding;
import zzr.library.databinding.DialogReturnBinding;
import zzr.library.db.common.AppDatabase;
import zzr.library.db.common.DBUtil;
import zzr.library.db.obj.BookDO;
import zzr.library.db.obj.BorrowDO;
import zzr.library.db.obj.ReaderDO;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    private AppDatabase appDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        appDatabase = DBUtil.getAppDatabase(this);


        binding.loginBtn.setOnClickListener(v -> {
            DialogLoginBinding dialogLoginBinding = DialogLoginBinding.inflate(getLayoutInflater());
            new AlertDialog.Builder(MainActivity.this)
                    .setView(dialogLoginBinding.getRoot())
                    .setTitle("登录")
                    .setPositiveButton("确认", (dialog, which) -> {
                        int sid = 0;
                        try {
                            sid = Integer.parseInt(dialogLoginBinding.loginSid.getText().toString());
                        } catch (NumberFormatException e) {
                            Snackbar.make(binding.getRoot(), "键入正确的学号！", Snackbar.LENGTH_LONG).show();
                        }
                        String pwd = dialogLoginBinding.loginPwd.getText().toString();
                        ReaderDO readerDO = appDatabase.readerDAO().selectBySidAndPwd(sid, pwd);
                        if (readerDO == null) {
                            Snackbar.make(binding.getRoot(), "学号或者密码有误！", Snackbar.LENGTH_LONG).show();
                        } else {
                            Intent intent = new Intent(this, ReaderActivity.class);
                            intent.putExtra("rid", readerDO.rid);
                            startActivity(intent);
                        }
                    }).create().show();
        });

        binding.returnBookBtn.setOnClickListener(v -> {
            DialogReturnBinding dialogReturnBinding = DialogReturnBinding.inflate(getLayoutInflater());
            new AlertDialog.Builder(this)
                    .setView(dialogReturnBinding.getRoot())
                    .setTitle("还书")
                    .setPositiveButton("归还此书", (dialog, which) -> {
                        int bid = 0;
                        try {
                            bid = Integer.parseInt(dialogReturnBinding.returnBid.getText().toString());
                        } catch (NumberFormatException e) {
                            Snackbar.make(binding.getRoot(), "键入正确的书号!", Snackbar.LENGTH_LONG).show();
                            return;
                        }

                        BookDO bookDO = appDatabase.bookDAO().selectById(bid);
                        bookDO.in = true;
                        appDatabase.bookDAO().update(bookDO);

                        BorrowDO borrowDO = appDatabase.borrowDAO().selectNotReturn(bid);
                        borrowDO.returned = true;
                        borrowDO.returnTime = LocalDateTime.now();
                        appDatabase.borrowDAO().update(borrowDO);

                        LocalDateTime dueTime = borrowDO.dueTime;
                        LocalDateTime now = LocalDateTime.now();
                        String status = "";
                        if (now.isAfter(dueTime)) {
                            status = "超期" + Duration.between(dueTime, now).toDays() + "天 ";
                        }

                        Snackbar.make(binding.getRoot(), status + "成功归还书籍：" + bookDO.name, Snackbar.LENGTH_LONG).show();
                    }).create().show();
        });

        if (appDatabase.readerDAO().selectById(1) == null) {
            create();
        }

        Intent intent = new Intent(this, ReaderActivity.class);
        intent.putExtra("rid", 1);
        startActivity(intent);

    }

    void create() {
        ReaderDO readerDO1 = new ReaderDO();
        readerDO1.className = "数字媒体171";
        readerDO1.name = "张峥润";
        readerDO1.pwd = "123";
        readerDO1.sid = 202170421;
        appDatabase.readerDAO().insert(readerDO1);
        for (int i = 1; i <= 6; i++) {
            BookDO bookDO = new BookDO(0, "972703440986", "疯狂Android讲义", "电子工业出版社", "602/32", false);
            appDatabase.bookDAO().insert(bookDO);

            BorrowDO borrowDO = new BorrowDO(0, i, 1, LocalDateTime.now(), LocalDateTime.now().plusDays(60), null, false);
            appDatabase.borrowDAO().insert(borrowDO);
        }
    }
}